import Vue from "vue";
import VueRouter from "vue-router";
import find from "./children/Find";
import home from "./children/Home";
import news from "./children/News";
import room from "./children/Room";
import user from "./children/User";

Vue.use(VueRouter);

const routes = [
  {
    // 重定向，设置默认路径
    // 子路由路径不要加 /
    path: "/",
    redirect: "/welcome",
  },
  {
    path: "/welcome",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Welcome.vue"),
    meta: {
      // 底部导航是否显示
      isNavShow: false,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/login",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login.vue"),
    meta: {
      // 底部导航是否显示
      isNavShow: false,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/login2",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login2.vue"),
    meta: {
      // 底部导航是否显示
      isNavShow: false,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/login3",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Login3.vue"),
    meta: {
      // 底部导航是否显示
      isNavShow: false,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/home",
    // name: "Home",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Home.vue"),
    children: home,
    meta: {
      // 底部导航是否显示
      isNavShow: true,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/room",
    // name: "Room",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Room.vue"),
    children: room,
    meta: {
      // 底部导航是否显示
      isNavShow: true,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/news",
    // name: "News",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/News.vue"),
    children: news,
    meta: {
      // 底部导航是否显示
      isNavShow: true,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/find",
    // name: "Find",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/Find.vue"),
    children: find,
    meta: {
      // 底部导航是否显示
      isNavShow: true,
      // 是否需要登录才能查看
      isLogin: false,
    },
  },
  {
    path: "/user",
    // name: "User",
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/User.vue"),
    children: user,
    meta: {
      // 底部导航是否显示
      isNavShow: false,
      // 是否需要登录才能查看
      isLogin: true,
    },
  },
];

const router = new VueRouter({
  // mode: "history",
  // base: process.env.BASE_URL,
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    let uid = localStorage.getItem("uids");
    let phone = localStorage.getItem("phone");
    uid && phone ? next() : next("/login");
  } else {
    next();
  }
});

export default router;
